/**
 * Created by Hata Kokoro on 6/28/2017.
 */

var Poker=require('../Poker/Poker.js');
var uTinsatance = require('./util.js');
var Pattern = require('./const.js').Pattern;
module.exports =
{
    /**
     * 检查创建房间的参数
     * @param args
     * @return -1 表示参数不正确，
     */
    checkCreateRoomArgs: function(args) {
        if (!args.gameType || !args.subType) {
            return false;
        }
        return true;
    },

    /**
     * 创建房间需要消耗的资源
     * @param args
     */
    calcCreateRoomNeedResource: function(args) {
        return {};
    },

    Color: {
        'SPADE': 2,          // 黑桃
        'HEART': 1,          // 红桃
        'CLUB': 4,          // 梅花
        'DIAMOND': 3       // 方块
    },

    ColorPower:[3,4,1,2],

    // hands:{Key:cards,value:number }
    _getCardPower:function(value){
        var point = Poker.getPoint(value) ;
        if(point>10){
            point=10;
        }
        return point;
    },

    /**
     * compare cards color
     * @param card1
     * @param card2
     * @returns {boolean}  true: card1 > card2  false:card1< card2
     * @private
     */
    _compareColor:function(card1,card2) {
        var color1 = Poker.getColor(card1);
        var color2 = Poker.getColor(card2);
        if (this.ColorPower[color1] > this.ColorPower[color2])
            return true;
        else return false;
    },
    /**
     * Calculate Hands Power
     * @param hands
     * @returns {number}
     */
    GetHandsPower:function(hands){
        var sumPower=0;
        for(var i=0;i<3;i++){
            sumPower+=this._getCardPower(hands[i]);
        }
        return sumPower;
    },

    /**
     * is Players hands has Niu
     * @param hands{k-v}
     * @constructor
     */
    HasNiu:function(cvTable){
        var Keys=Object.keys(cvTable);
        for(var i=0;i<Keys.length;i++){
            var pr = this.GetHandsPower(cvTable[Keys[i]]);
            if(pr%10==0){
                return true;
            }
        }
        return false;
    },

    /**
     *
     * @param objCard
     * @returns {boolean}
     */
    isNiu:function(objCard){
        var  arrCard = objCard
        var i=0
            ,sum=0;
        for(i;i<3;i++){
            sum+=this._getCardPower(arrCard[i]);
        }
        if(sum%10==0) {
            return true;
        }
        return false;
    },


    /**
     *Calculate powerRating of unselected cards
     * @param objCard
     * @returns {number}
     */
    GetSecondPattern:function(objCard) {
        var arrCard = objCard
        var s1 = this._getCardPower(arrCard[0]);
        var s2 = this._getCardPower(arrCard[1]);
        logger.debug("s1: "+s1); //4
        logger.debug("s2: "+s2); //8
        var sum = s1 + s2;
        //logger.debug("--debug:Get Second Pattern :")
        //logger.debug("sum: "+sum);
        //logger.debug("--debug:Get Second Pattern :")
        if(sum==10||sum==20){ return Pattern.N10}
        var n=sum%10;
        switch(n) {
            case 0:
                return Pattern.N10;
                break;
            case 9:
                return Pattern.N9;
                break;
            case 8:
                return Pattern.N8;
                break;
            case 7:
                return Pattern.N7;
                break;
            case 6:
                return Pattern.N6;
                break;
            case 5:
                return Pattern.N5;
                break;
            case 4:
                return Pattern.N4;
                break;
            case 3:
                return Pattern.N3;
                break;
            case 2:
                return Pattern.N2;
                break;
            case 1:
                return Pattern.N1;
                break;
        }
    },
    /**
     *Compare dealer and normal players Max single card
     * @param [object]dealerHands
     * @param [object]normalHands
     * @returns {boolean} true:normal wins false:dealer wins
     */
    CompareSingleCard: function (dealerHands,normalHands) {
        var arrDealer=uTinsatance.Hands2Array(dealerHands);
        var arrNormal=uTinsatance.Hands2Array(normalHands);
        var dealerPoint = uTinsatance.GetArrayMax(arrDealer);
        var normalPoint=uTinsatance.GetArrayMax(arrNormal);
        if(dealerPoint>normalPoint) {
            return false;
        }else if(dealerPoint<normalPoint){
            return true;
        }else if(dealerPoint==normalPoint){
            // true: card1 > card2  false:card1< card2
           return  this._compareColor(normalPoint,dealerPoint);
        }
    },

    _sortCard: function (i_Arr) {
        var arrValue = new Array();
        for(var i=0;i<i_Arr.length;i++){
            var point =Poker.getPoint(arrValue[i]);
            arrValue.push(point);
        }
        this.arr_sort(i_Arr);
    },

    /**
     * Sort the Array
     * @param i_Arr
     */
    arr_sort : function (i_Arr) {
        for (var i = 0; i < i_Arr.length; i++) {
            for (var j = i; j < i_Arr.length; j++) {
                if (i_Arr[i] > i_Arr[j]) {
                    var tmp = i_Arr[i];
                    i_Arr[i] = i_Arr[j];
                    i_Arr[j] = tmp;
                }//end if
            }//end for
        }//end for
    },
    /**
     * Check if the (object)Cards is a Special Pattern
     * and Return the (int)Pattern Value
     * if can't made up any type of special pattern return 0
     *  @param objCards
     * @returns {number}
     * @Public_Method
     */
    SpecialPatternCheck: function (objCards) {
        var arrCards = uTinsatance.Hands2Array(objCards);
        if (this._isDragon(arrCards))
            return Pattern.DRAGON;
        if(this._isBomb(objCards)){
            return Pattern.BOMB;
        }
        if(this._isFiveCalf(arrCards)){
            return Pattern.FIVE_CALF;
        }
        if(this._isGoldBull(arrCards)){
            return Pattern.GOLDEN_COW;
        }
        if(this._isSliverBull(arrCards)){
            return Pattern.SLIVER_COW;
        }
        return 0;
    },
    /**
     * @param arrCards
     * @returns {boolean}
     * @private
     */
    _isDragon: function (arrCards) {
        this._sortCard(arrCards);
        if (Poker.getPoint(arrCards[0]) == 1 &&
            Poker.getPoint(arrCards[1]) == 2 &&
            Poker.getPoint(arrCards[2]) == 3 &&
            Poker.getPoint(arrCards[3]) == 4 &&
            Poker.getPoint(arrCards[4]) == 5) {
            return true;
        } else
            return false;
    },
    /**
     * Bomb (炸弹）
     * @param objCards
     * @returns {boolean}
     * @private
     */
    _isBomb: function (objCards) {
        var ret = false;
        var arrPts=[]
        var ptsDistri = {}
        ExObject.eachKeyNum(objCards, function (card, num) {
            var point = Poker.getPoint(card);
            arrPts.push(point);
        });
        for(var i=0;i<arrPts.length;i++){
            var e=arrPts[i]
            var v =ptsDistri[e]
            if(v){
                ptsDistri[e]+=1
            }else{
                ptsDistri[e]=1
            }
        }
        ExObject.eachKeyNum(ptsDistri,function(pts,qua){
            if(qua==4) {
                this.ret = true;
                return true;
            }
        }.bind(this));
        return ret;
    },
    /**
     * Five Calf（五小牛）
     * @param arrCards
     * @returns {boolean}
     * @private
     */
    _isFiveCalf: function (arrCards) {
        var sum = 0;
        for (var i = 0; i < 5; i++) {
            sum += this._getCardPower(arrCards[i]);
        }
        if (sum <= 10)
            return true;
        else return false;
    },
    /**
     * Gold Bull （金牛）
     * @param arrCards
     * @returns {boolean}
     * @private
     */
    _isGoldBull: function (arrCards) {
        var ret = true;
        for (var i = 0; i < 5; i++) {
            if (this._getCardPower(arrCards[i])!= 10) {
                ret = false;
                break;
            }
        }
        return ret;
    },
    /**
     * Sliver Bull （银牛）
     * @param arrCards
     * @returns {boolean}
     * @private
     */
    _isSliverBull: function (arrCards) {
        var ret =true;
        for(var i=0;i<5;i++){
            if(this._getCardPower(arrCards[i])<10) {
                ret = false;
            }
        }
        return ret;
    }

}

